var $grid = $('#bt_plan_grid');
var viewDialog=$('#dd');

context.ready = function() {
    $grid.datagrid({
        fit: true,
        border: true,
        // width:'95%',
        // height:'500',
        nowrap: true,
        autoRowHeight: true,
        striped: true,
        collapsible:true,
        url: _ROOT_ + '/plan/getData',
        pagination: true,
        columns: [[
                {field:'ck',checkbox: true}, 
                // {field: 'ID', title: 'ID', width: 100, align: 'center'},
                // {field: '邮件时间', title: '邮件时间' ,align: 'center',width: 150},
                // {field: '需求品种', title: '需求品种' ,align: 'center'},
                // {field: '需求数量', title: '需求数量' ,align: 'center'},
                // {field: 'saname', title: '客户简称' ,align: 'center',width: 150},
                // {field: 'name', title: '客户信息' ,align: 'center',width: 150},
                // {field: '开始时间', title: '开始时间' ,align: 'center'},
                // {field: '截止时间', title: '截止时间' ,align: 'center'},
                // {field: '实际发货数量', title: '实际发货数量' ,align: 'center'},
                // {field: 'status',  title: '状态', width: 100, align: 'center', formatter: function(value) {
                //         if (value === 'P') {
                //             return '可用';
                //         }
                //         return '<font color="red">禁用</font>';
                // }},
                // {field: '备注', title: '备注', width: 200},
                {field: 'ID', title: '操作',  align: 'center', formatter: function(value) {
                                        return '<span title="编辑" class="img-btn icon-edit" ID=' + value + '></span>';
                }}

            ]],
        toolbar: [{
                text: '新增',
                iconCls: 'icon-add',
                handler: context.addView
            }, {
                text: '删除',
                iconCls: 'icon-remove',
                handler: context.doDelete
            }],
        onLoadSuccess: function() {
            var $bodyView = $grid.data('datagrid').dc.view2;
            $bodyView.find('span[ID]').click(function(e) {
                e.stopPropagation();
                var uid = $(this).attr('ID');
                context.updateView(uid);
            });
        },
        onDblClickRow: function(rowIndex, rowData) {

            // if (c_id && c_id === rowData.c_id) {
            //     return;
            // }
            var $layout = $('#bt_plan_layout');
            var south = $layout.layout('panel', 'south');
            if (south.panel('options').collapsed) {
                $layout.layout('expand', 'south');
            }

            // $.alert(rowData.c_id);
            $khGrid.datagrid({
                url: _ROOT_ + '/plan/getinfData',
                queryParams: {c_id: rowData.c_id}
            });
        }
    });

context.addView = function() {

    viewDialog.dialog({

        modal: true,  
        title: '新增订单',
        href: _ROOT_ + '/plan/toadd',
        width: 300,
        height: 400,    
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            }]
    });
};

context.addcfView = function() {

    viewDialog.dialog({

        modal: true,  
        title: '新增订单',
        href: _ROOT_ + '/plan/tocfadd',
        width: 300,
        height: 400,    
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            }]
    });
};


context.updateView = function(id) {
//  $.alert(id);
    viewDialog.dialog({
        title: '编辑出口清单',
        href: _ROOT_ + '/plan/toUpdate?ID=' + id,
        width: 400,
        height: 500,
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            },]
    });
};

context.doDelete = function() {
    var checked = $grid.datagrid('getChecked');
    if (checked && checked.length > 0) {
        $.confirm('确认删除？', function(r) {
            if (r) {
                var ids = [];
                $.each(checked, function() {
                    ids.push(this.ID);
                });
                $.post(_ROOT_ + '/plan/doDelete', {ids: ids.join(',')}, function(rsp) {
                    if (rsp.status) {
                        $grid.datagrid('reload');
                    } else {
                        $.alert(rsp.msg);
                    }
                }, 'JSON');
            }
        });
    }
};

context.doSubmit = function() {
    $bt_plan_from = $('#bt_plan_from');
    // $.alert("sdfsdf");
    if ($bt_plan_from.form('validate')) {
        $.post(_ROOT_ + '/plan/doSave', $bt_plan_from.toJson(), function(rsp) {
            // $.alert(rsp.status);
            if (rsp.status) {
                $grid.datagrid('reload');
                
        viewDialog.dialog('close');
            } else {
                $.alert(rsp.msg);
            }
        }, "JSON");
    }
};
}